package cars

import (
	"parking-lot-go-dingding/model"
	"time"

	"gorm.io/gorm"
)

type CarsStruct struct {
	Id                int       `json:"id" gorm:"type:int(11) unsigned auto_increment; NOT NULL; primary_key"`
	TripartiteType    int       `json:"tripartite_type" gorm:"type:tinyint(11); COMMENT:'账号类型'"`
	TripartiteVoucher string    `json:"tripartite_voucher" gorm:"type:varchar(255); COMMENT:'三方凭证'"`
	MemberId          int       `json:"member_id" gorm:"type:int(11); COMMENT:'member表id'"`
	Mobile            string    `json:"mobile" gorm:"type:varchar(255); COMMENT:'手机号'"`
	CarNumber         string    `json:"car_number" gorm:"type:varchar(20); COMMENT:'车牌号'"`
	CarInfo           string    `json:"car_info" gorm:"type:varchar(50); COMMENT:'车辆信息'"`
	CarType           int       `json:"car_type" gorm:"type:tinyint(1); COMMENT:'车型 1: 小型车 2: 中型车 3: 大型车 4: 其 他'"`
	CarNoColor        int       `json:"car_no_color" gorm:"type:tinyint(1); COMMENT:'车牌颜色 1-蓝牌 3-黄牌 6-绿牌 7-白牌 8-黑牌'"`
	From              int       `json:"from" gorm:"type:tinyint(1); COMMENT:'来源类型'"`
	BindType          int       `json:"bind_type" gorm:"type:tinyint(1); COMMENT:'绑定类型 1永久 2临时'"`
	StartTime         int       `json:"start_time" gorm:"type:int(11); COMMENT:'绑定开始时间'"`
	EndTime           int       `json:"end_time" gorm:"type:int(11); COMMENT:'绑定结束时间'"`
	CreatedAt         time.Time `json:"created_at" gorm:"type:datetime"`
	UpdatedAt         time.Time `json:"updated_at" gorm:"type:datetime"`
}

// 车牌号
func (c *CarsStruct) TableName() string {
	return "cars"
}

// 列表查询
func (c *CarsStruct) List(where interface{}) ([]CarsStruct, error) {
	var carsStruct []CarsStruct
	var db, err = model.BuildWhere(where)
	if err != nil {
		return nil, err
	}
	if err := db.Debug().Table(c.TableName()).Scan(&carsStruct).Error; err != nil {
		//没查到记录
		if err == gorm.ErrRecordNotFound {
			return carsStruct, nil
		} else {
			return nil, err
		}
	}

	return carsStruct, nil
}

// 关键词查询车牌号ids
func (c *CarsStruct) CarNumberFind(keywords string) ([]int, error) {

	var carsStruct []CarsStruct
	if err := model.Db.Table(c.TableName()).Where("car_number like ?", "%"+keywords+"%").Scan(&carsStruct).Error; err != nil {
		//没查到记录
		if err == gorm.ErrRecordNotFound {
			return []int{}, nil
		} else {
			return nil, err
		}
	}

	var memberIds []int
	for _, v := range carsStruct {
		memberIds = append(memberIds, v.MemberId)
	}

	return memberIds, nil
}

func (c *CarsStruct) Create(car CarsStruct) error {
	var carsStruct CarsStruct
	if err := model.Db.Table(c.TableName()).Create(&carsStruct).Error; err != nil {
		return err
	}

	return nil
}

// 会员车辆
func (c *CarsStruct) MemberCarCount(memberId int) (int64, error) {
	var count int64

	if err := model.Db.Table(c.TableName()).Where("member_id", memberId).Count(&count).Error; err != nil && err != gorm.ErrRecordNotFound {
		return 0, err
	}

	return count, nil
}

// 根据用户id查询车辆
func (c *CarsStruct) FirstByMemberId(memberId int) (CarsStruct, error) {
	var carsStruct CarsStruct
	if err := model.Db.
		Table(c.TableName()).
		Where("member_Id", memberId).
		First(&carsStruct).
		Error; err != nil {
		return carsStruct, err
	}
	return carsStruct, nil
}

func (c *CarsStruct) Count(where map[string]interface{}) (int64, error) {
	var count int64

	if err := model.Db.Table(c.TableName()).Where(where).Count(&count).Error; err != nil {
		return 0, err
	}
	return count, nil
}

// 关键词查询车牌号ids
func (c *CarsStruct) Find(where map[string]interface{}) (CarsStruct, error) {
	var carsStruct CarsStruct
	if err := model.Db.Table(c.TableName()).Where(where).Scan(&carsStruct).Error; err != nil {
		//没查到记录
		if err == gorm.ErrRecordNotFound {
			return carsStruct, nil
		} else {
			return carsStruct, err
		}
		return carsStruct, err
	}
	return carsStruct, nil

}
